/**
 * 切换企业和库视图需再次执行的代码
 */

import { Tag } from '@teamix/ui';
import { navigate } from 'utils/router';
import utils from 'utils/index';
import Searcher from './searcher';
import Collect from './collect';
import BreadcrumbOverlay from './breadcrumbOverlay';
import intl from '../../locale';
import { getFeature } from 'utils/features';
const { pathResource } = window.FORCE;

export default function initAboutOrganizationOrProject() {
  const { isProject, isProjectOrGroup } = utils.projectOrGroup(pathResource);
  window.location.pathname !== '/search' && getFeature('page_enable.org.search') &&
    window.TB_NAVIGATION.Searcher.register({
      placeholder: '搜代码、提交、文件等',
      plugin: Searcher,
      action: 'dropdown',
    });

  if (isProjectOrGroup) {
    const isArchive = isProjectOrGroup ? pathResource.isArchive : false;
    let dataSource;

    if (pathResource.fullName) {
      const fullNameArr = pathResource.fullName.split(' / ');
      const fullPathArr = pathResource.fullPath.split('/');

      // 公有云fullName的第一级是企业别名或id，需要去掉
      fullNameArr.shift();
      const orgPath = fullPathArr.shift();

      dataSource = fullNameArr.map((item, key) => {
        if (key === fullNameArr.length - 1) {
          return {
            title: <span>{item}</span>,

            key: fullPathArr.join('/'),
            // 最后一级面包屑支持切换同级库、组
            needDropDown: true,
            plugin: BreadcrumbOverlay,
            width: 300,
            action: 'dropdown',
          };
        }

        return {
          title: item,
          key: fullPathArr.slice(0, key + 1).join('/'), // 以这一级的路径作为key
        };
      });

      window.TB_NAVIGATION.ContextSwitcher.register({
        dataSource,
        onClick({ key }) {
          // 每级item（除最后一级）被点击的callback
          if (key === fullPathArr.join('/')) return;
          navigate(`/${orgPath}/${key}`, {
            state: { forceUpdate: true },
          });
        },
      });
    } else {
      dataSource = [
        {
          title: 'Codeup',
        },
      ];

      window.TB_NAVIGATION.ContextSwitcher.register({
        dataSource,
        onClick() {
          navigate('/', {
            state: { forceUpdate: true },
          });
        },
      });
    }

    // 库视图下展示归档标签
    if (isArchive === 'true') {
      window.TB_NAVIGATION.CustomAction.register([
        {
          element: (
            <Tag color="red" prefix="yx-" size="small" style={{ margin: '-1px 4px 0 0' }}>
              {intl.get({
                id: 'code-assets.utils.initNavigation.initAboutOrganizationOrProject.Archived',
                defaultMessage: '已归档',
              })}
            </Tag>
          ),

          action: 'custom',
          trigger: '',
        },
      ]);
    }

    // 库视图下展示收藏按钮（无权限vm增加了id和type）
    if (isProject && pathResource.fullName) {
      window.TB_NAVIGATION.CustomAction.register([
        {
          element: <Collect />,
          action: 'custom',
          trigger: '',
        },
      ]);
    } else {
      window.TB_NAVIGATION.CustomAction.register([
        {
          element: null,
          action: 'custom',
          trigger: '',
        },
      ]);
    }
  } else if (window.location.pathname.indexOf('/search') === 0) {
    window.TB_NAVIGATION.setHomePageFlag(false);
    const dataSource = [
      {
        title: '高级搜索',
      },
    ];

    window.TB_NAVIGATION.ContextSwitcher.register({
      dataSource,
      onClick() {
        navigate('/', {
          state: { forceUpdate: true },
        });
      },
    });
  } else {
    // 企业-设置
    if (window.location.pathname.indexOf('/settings/') === 0) {
      window.TB_NAVIGATION.setHomePageFlag(false);
      const dataSource = [
        {
          title: intl.get({
            id: 'code-assets.utils.initNavigation.initAboutOrganizationOrProject.Set',
            defaultMessage: '设置',
          }),
        },
      ];

      window.TB_NAVIGATION.ContextSwitcher.register({
        dataSource,
        onClick() {
          navigate('/', {
            state: { forceUpdate: true },
          });
        },
      });
    }
    // 企业下不展示库维度的收藏和归档
    window.TB_NAVIGATION.CustomAction.register([
      {
        element: null,
        action: 'custom',
        trigger: '',
      },
    ]);
  }
}
